using System;
using System.ComponentModel.Composition;
using System.Windows;
using MediaAssistant.DAL.Helper;
using MediaAssistant.Management;
using MefBasic;
using MefBasic.Commans;
using Microsoft.Practices.Composite.Events;

namespace MediaAssistant.Controls.Dialog
{
    [Export(typeof(IDialogPresenter))]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class DialogPresenter:APresenter<IDialogView>, IDialogPresenter
    {
        [ImportingConstructor]
        public DialogPresenter(IDialogView view) : base(view)
        {
            _view = view;
            _dialogResult = DialogResult.Cancel;
            DialogCommand = new DelegateCommand(ExecuteDialogCommand);
            _view.Closing += ViewClosing;
        }

        [Import]
        private IEventAggregator EventAggregator { get; set; }

        public string OkButtonCaption
        {
            get { return (string)GetValue(OkButtonCaptionProperty); }
            set { SetValue(OkButtonCaptionProperty, value); }
        }

        public static readonly DependencyProperty OkButtonCaptionProperty =
            DependencyProperty.Register("OkButtonCaption", typeof(string), typeof(DialogPresenter), new UIPropertyMetadata("Ok"));


        public string CancelButtonCaption
        {
            get { return (string)GetValue(CancelButtonCaptionProperty); }
            set { SetValue(CancelButtonCaptionProperty, value); }
        }

        public void ShowDialog(string title, IDialogContentPresenter contentPresenter, bool showDialogButtons)
        {
            DialogButtonVisibility = showDialogButtons ? Visibility.Visible : Visibility.Collapsed;
            ShowDialog(title, contentPresenter);
        }



        public Visibility DialogButtonVisibility
        {
            get { return (Visibility)GetValue(DialogButtonVisibilityProperty); }
            set { SetValue(DialogButtonVisibilityProperty, value); }
        }

        public static readonly DependencyProperty DialogButtonVisibilityProperty =
            DependencyProperty.Register("DialogButtonVisibility", typeof(Visibility), typeof(DialogPresenter), new UIPropertyMetadata(Visibility.Visible));



        public static readonly DependencyProperty CancelButtonCaptionProperty =
            DependencyProperty.Register("CancelButtonCaption", typeof(string), typeof(DialogPresenter), new UIPropertyMetadata("Cancel"));

        

        void ViewClosing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (_executingCommand) return;
            if (ContentPresenter.OnClosing(DialogResult.Cancel))
            {
                _dialogResult = DialogResult.Cancel;
            }
            else
            {
                e.Cancel = true;
            }
        }

        public DelegateCommand DialogCommand { get; set; }

        private void ExecuteDialogCommand(object obj)
        {
            _executingCommand = true;
            var dialogResult = (DialogResult)Enum.Parse(typeof(DialogResult), obj.ToString());
            if (ContentPresenter.OnClosing(dialogResult))
            {
                _dialogResult = dialogResult;
                _view.Close();
            }
            _executingCommand = false;
        }

        public static readonly DependencyProperty ContentPresenterProperty = DependencyProperty.Register(
            "ContentPresenter",
            typeof(IDialogContentPresenter),
            typeof(DialogPresenter));

        public IDialogContentPresenter ContentPresenter
        {
            get { return (IDialogContentPresenter)GetValue(ContentPresenterProperty); }
            set { SetValue(ContentPresenterProperty, value); }
        }

        public static readonly DependencyProperty TitleProperty = DependencyProperty.Register(
            "Title",
            typeof(string),
            typeof(DialogPresenter));
        public string Title
        {
            get { return (string)GetValue(TitleProperty); }
            set { SetValue(TitleProperty, value); }
        }

        private readonly IDialogView _view;
        private DialogResult _dialogResult;
        private bool _executingCommand;

        public DialogResult ShowDialog(string title, IDialogContentPresenter contentPresenter)
        {
            Logger.Instance.Write(string.Format("Showing Dialog with title {0} With Presenter {1}",title,contentPresenter));
            Title = title;
            ContentPresenter = contentPresenter;
            EventAggregator.GetEvent<BaseEvent<ShellDisabledChangedEventArgs>>().Publish(new ShellDisabledChangedEventArgs{Sender = this,Disabled = true});
            _view.ShowModal();
            EventAggregator.GetEvent<BaseEvent<ShellDisabledChangedEventArgs>>().Publish(new ShellDisabledChangedEventArgs { Sender = this, Disabled = false });
            Logger.Instance.Write(string.Format("Closing Dialog with title {0} With Presenter {1}. Returning {2}", title, contentPresenter,_dialogResult));
            return _dialogResult;
        }
    }
}
